﻿using System.Net;
using System.Text;

namespace Common
{
    public static class MessageHelper
    {
        /// <summary>
        /// 提交发送请求
        /// </summary>
        /// <param name="uri">提交地址</param>
        /// <param name="value">提交的JSON字符串</param>
        /// <returns>API的返回信息</returns>
        private static string posturi(string uri, string value)
        {
            Encoding encoding = Encoding.UTF8;
            WebClient wc = new WebClient();
            byte[] postData = encoding.GetBytes(value);

            // 得到返回字符流
            byte[] responseData = wc.UploadData(uri, "POST", postData);

            // 解码
            return encoding.GetString(responseData);
        }

        /// <summary>
        /// 提交发送短信
        /// </summary>
        /// <returns></returns>
        public static string send(string mobile, string content)
        {
            string uri = "http://api.lingchuangyun.cn/send";
            send temp = new send()
            {
                mobile = mobile,               //  您发送的手机号已,逗号隔开
                content = content,                   //  【签名】您的发送内容
                genre = "4"                          //  您发送的短信类型
            };
            return posturi(uri, Newtonsoft.Json.JsonConvert.SerializeObject(temp));
        }

        /// <summary>
        /// 验证短信内容是否合法
        /// </summary>
        /// <returns></returns>
        private static string checkcontent()
        {
            string uri = "http://check.lingchuangyun.cn/checkcontent";
            checkcontent temp = new checkcontent()
            {
                content = "【签名】您的发送内容"                    //  【签名】您的发送内容
            };

            return posturi(uri, Newtonsoft.Json.JsonConvert.SerializeObject(temp));
        }

        /// <summary>
        /// 查询短信余量
        /// </summary>
        /// <returns></returns>
        private static string surplus()
        {
            string uri = "http://check.lingchuangyun.cn/surplus";
            surplus temp = new surplus()
            {
                genre = "4-营销类"                      //  您发送的短信类型
            };

            return posturi(uri, Newtonsoft.Json.JsonConvert.SerializeObject(temp));
        }
    }

    /// <summary>
    /// app信息
    /// </summary>
    public class app
    {
        public string appid = "376b405249f525c8";   // 您在短信平台生成的APPID
        public string secret = "cc62da4a51b035c4e3cc4954db5c11c4";  //  您在短信平台生成的APPID对应的SECRET
    }

    /// <summary>
    /// 发送短信类
    /// </summary>
    public class send : app
    {
        public string mobile { get; set; }  //  您发送的手机号已,逗号隔开
        public string content { get; set; } //  【签名】您的发送内容
        public string genre { get; set; }   //  您发送的短信类型
    }

    /// <summary>
    /// 验证短信内容是否合法
    /// </summary>
    public class checkcontent : app
    {
        public string content { get; set; } //  【签名】您的发送内容
    }

    /// <summary>
    /// 查询短信余量
    /// </summary>
    public class surplus : app
    {
        public string genre { get; set; }   //  您发送的短信类型
    }

}